<?php

namespace common\models\customInfo;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\customInfo\CustomInfo;

/**
 * CustomInfoSearch represents the model behind the search form about `common\models\customInfo\CustomInfo`.
 */
class CustomInfoSearch extends CustomInfo
{   
    public $sub_info1;
    public $sub_info2;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['info_id', 'ac_id', 'user_id', 'status', 'code'], 'integer'],
            [['token', 'wecha_id', 'add_time', 'user_name', 'phone', 'sub_info','sub_info1','sub_info2', 'work'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params,$id)
    {
        $query = CustomInfo::find()->where(['ac_id' => $id])->andWhere(['<>','status',0]);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'info_id' => $this->info_id,
            'ac_id' => $this->ac_id,
            'user_id' => $this->user_id,
            'status' => $this->status,
            'code' => $this->code,
        ]);
        $info_id = $params['id'];
        //如果搜索的是姓名
        if(!empty($params['CustomInfoSearch']['sub_info']))
        {
            $list = Yii::$app->db->createCommand("select info_id,sub_info from custom_info where ac_id=".$info_id)->queryAll();
            $sub_info = array();
            foreach ($list as $key => $value) {
                foreach (unserialize($value['sub_info']) as $key => $val) {
                    if($val['name']=="姓名" && $params['CustomInfoSearch']['sub_info1']==$val['value'])
                    {
                        $sub_info['info_id']=$value['info_id'];
                        //搜索的infoid
                        $sousuoid = $sub_info['info_id'];
                    }
                }
            }
            $query->andFilterWhere(['in', 'info_id', $sousuoid]);
            return $dataProvider;
        }
        //如果搜索的是手机号
        elseif(!empty($params['CustomInfoSearch']['sub_info1']))
        {
            $list = Yii::$app->db->createCommand("select info_id,sub_info from custom_info where ac_id=".$info_id)->queryAll();
            $sub_info = array();
            foreach ($list as $key => $value) {
                foreach (unserialize($value['sub_info']) as $key => $val) {
                    if($val['type']=="phone" && $params['CustomInfoSearch']['sub_info1']==$val['value'])
                    {
                        $sub_info['info_id']=$value['info_id'];
                        //搜索的infoid
                        $sousuoid[] = $sub_info['info_id'];
                    }
                }
            }
            $query->andFilterWhere(['in', 'info_id', $sousuoid]);
            return $dataProvider;
        }
        //如果搜索的是邮箱
        elseif(!empty($params['CustomInfoSearch']['sub_info2']))
        {
            $list = Yii::$app->db->createCommand("select info_id,sub_info from custom_info where ac_id=".$info_id)->queryAll();
            $sub_info = array();
            foreach ($list as $key => $value) {
                foreach (unserialize($value['sub_info']) as $key => $val) {
                    if($val['type']=="email" && $params['CustomInfoSearch']['sub_info1']==$val['value'])
                    {
                        $sub_info['info_id']=$value['info_id'];
                        //搜索的infoid
                        $sousuoid[] = $sub_info['info_id'];   
                    }
                }
            }
            $query->andFilterWhere(['in', 'info_id', $sousuoid]);
            return $dataProvider;  
        }
        else
        {
           $query->andFilterWhere(['like', 'token', $this->token])
            ->andFilterWhere(['like', 'wecha_id', $this->wecha_id])
            ->andFilterWhere(['like', 'add_time', $this->add_time])
            ->andFilterWhere(['like', 'user_name', $this->user_name])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'work', $this->work]); 
        }
        

        return $dataProvider;

    }
}
